package com.pig4cloud.pig.yin.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.security.annotation.Inner;
import com.pig4cloud.pig.yin.entity.WzContactUserEntity;
import com.pig4cloud.pig.yin.service.WzContactUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 联络人信息表
 *
 * @author pig
 * @date 2025-07-29 14:44:15
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/wzContactUser" )
@Tag(description = "wzContactUser" , name = "联络人信息表管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class WzContactUserController {

    private final WzContactUserService wzContactUserService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param wzContactUser 联络人信息表
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    //@PreAuthorize("@pms.hasPermission('pig_wzContactUser_view')" )
    public R getWzContactUserPage(@ParameterObject Page page, @ParameterObject WzContactUserEntity wzContactUser) {
        LambdaQueryWrapper<WzContactUserEntity> wrapper = Wrappers.lambdaQuery();
        return R.ok(wzContactUserService.page(page, wrapper));
    }


    /**
     * 通过id查询联络人信息表
     * @param userId id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{userId}" )
    //@PreAuthorize("@pms.hasPermission('pig_wzContactUser_view')" )
    public R getById(@PathVariable("userId" ) Long userId) {
        return R.ok(wzContactUserService.getById(userId));
    }

    /**
     * 新增联络人信息表
     * @param wzContactUser 联络人信息表
     * @return R
     */
    @Operation(summary = "新增联络人信息表" , description = "新增联络人信息表" )
    @SysLog("新增联络人信息表" )
    @PostMapping
    //@PreAuthorize("@pms.hasPermission('pig_wzContactUser_add')" )
    public R save(@RequestBody WzContactUserEntity wzContactUser) {
        return R.ok(wzContactUserService.save(wzContactUser));
    }

    /**
     * 修改联络人信息表
     * @param wzContactUser 联络人信息表
     * @return R
     */
    @Operation(summary = "修改联络人信息表" , description = "修改联络人信息表" )
    @SysLog("修改联络人信息表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('pig_wzContactUser_edit')" )
    public R updateById(@RequestBody WzContactUserEntity wzContactUser) {
        return R.ok(wzContactUserService.updateById(wzContactUser));
    }

    /**
     * 通过id删除联络人信息表
     * @param ids userId列表
     * @return R
     */

    @Operation(summary = "通过id删除联络人信息表" , description = "通过id删除联络人信息表" )
    @SysLog("通过id删除联络人信息表" )
    @DeleteMapping
  @PreAuthorize("@pms.hasPermission('pig_wzContactUser_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(wzContactUserService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param wzContactUser 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('pig_wzContactUser_export')" )
    public List<WzContactUserEntity> export(WzContactUserEntity wzContactUser,Long[] ids) {
        return wzContactUserService.list(Wrappers.lambdaQuery(wzContactUser).in(ArrayUtil.isNotEmpty(ids), WzContactUserEntity::getUserId, ids));
    }
}